package com.java.wx.controller;

import com.java.bean.*;
import com.java.mvc.Body;
import com.java.service.ExpressService;
import com.java.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class CourierController {

//    @Body("/wx/isCourier.do")
//    public String isCourier(HttpServletRequest req, HttpServletResponse resp) {

    @Body("/courier/getHistory.do")
    public String getHistory(HttpServletRequest req, HttpServletResponse resp) {
        Courier courier = UserUtil.getCourier(req);
        if (courier == null) {
            return new Message(-3).toJSON();
        }
        String phone = courier.getPhone();
        if (phone == null) {
            return new Message(-2).toJSON();
        }
        List<Express> list = ExpressService.getExpressBySysphone(phone);
        if (list == null || list.isEmpty()) {
            return new Message(-1).toJSON();
        }
        Object[] objects = list.stream().sorted(Comparator.comparing(Express::getIntime).reversed()).toArray();
        List<BootstrapTableExpress> result = new ArrayList<>();
        for (Object object : objects) {
            result.add(new BootstrapTableExpress((Express) object));
        }
        return new Message(0, result).toJSON();
    }

    @Body("/wx/isCourier.do")
    public String isCourier(HttpServletRequest req, HttpServletResponse resp) {
        final User user = UserUtil.getUser(req);
        if (user != null) {
            return new Message(0).toJSON();
        }
        if (UserUtil.getCourier(req) != null) {
            return new Message(1).toJSON();
        }
        return new Message(-1).toJSON();
    }
}
